﻿using System;
using System.Data;
using msBsc.AppLogic.DataAlc;

namespace msBsc.WebUi
{
    public partial class wfShpPageGoodsList : msBsc.Cls.BPageHt
    {

        //CBizShpCommercialTenantInfo objShpCommercialTenantInfo = new CBizShpCommercialTenantInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                this.txtColId.Value = this.GetFormParam("COL_ID");
                this.txtPageNo.Value = this.GetFormParam("PAGE_NO"); 
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
                //DataSet ds;
                //using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                //{
                //    ds = oCDbAccess.QueryBySQL("SELECT FLOOR_ID,MAIN_GOODS_NME FROM SHP_GOODS_FLOOR");
                //    ds.Tables[0].TableName = "SHP_GOODS_FLOOR";

                //}
                //BoundComboBox("FLOOR_ID", ds, "MAIN_GOODS_NME", "FLOOR_ID", "", "", true);
            }
        }
        //重构基类的回调处理函数，以strAction属性来判断是什么类型的回调，该变量的值对应前台的ACTION参数，如：按钮的名称等
        public override void InitialByParam()
        {
            if (this.gtGridRet != null)
            {
                if (this.gtGridRet.action == "load" || this.gtGridRet.action == "export")//处理GRID的数据原获取和数据导出
                {
                    DataSet ds = GetDisplayData(this.GetParamHtValue("gtGridID"), this.strAction, this.sCurrPageRadom);
                    if (ds != null && ds.Tables.Count > 0)
                        SetReturnDatatable(ds.Tables[0], "SRC_GOODS_ID");//CONSTANT_CATEGORY_ID为表的KEY字段，可填可不填
                }
                //处理数据保存
                if (this.gtGridRet.action == "save" && (gtGridRet.insertedRecords.Length > 0 || gtGridRet.updatedRecords.Length > 0 || gtGridRet.deletedRecords.Length > 0))
                {
                    DataSet ds = GetDisplayData(this.GetParamHtValue("gtGridID"), this.strAction, this.sCurrPageRadom);
                    try
                    {
                        if (ds != null && ds.Tables.Count > 0)
                        {
                            ////处理记录新增，前台已传入新增的ROW
                            //for (int i = 0; i < gtGridRet.insertedRecords.Length; i++)
                            //{
                            //    DataRow drNew = ds.Tables[0].NewRow();
                            //    msBsc.AppLogic.Cfg.HttpTools.CopyHashToDataRow(gtGridRet.insertedRecords[i], drNew);
                            //    drNew["PAGE_COL_ID"] = (new Epm.AppLogic.Framework.CDbAccess()).NewID();

                            //    drNew["PAGE_NO"] = this.GetParamHtValue("NO");
                            //    ds.Tables[0].Rows.Add(drNew);
                            //}

                            //处理记录更新，前台已传入更新的ROW
                            for (int i = 0; i < gtGridRet.updatedRecords.Length; i++)
                            {
                                string stridtm = gtGridRet.updatedRecords[i]["SRC_GOODS_ID"].ToString();
                                if (stridtm != string.Empty)
                                {
                                    DataRow[] arrtemr = ds.Tables[0].Select("SRC_GOODS_ID='" + stridtm + "'");
                                    if (arrtemr.Length > 0)
                                    {
                                        msBsc.AppLogic.Cfg.HttpTools.CopyHashToDataRow(gtGridRet.updatedRecords[i], arrtemr[0]);
                                    }
                                }
                            }

                            ////处理记录删除，前台已传入删除的ROW
                            //for (int i = 0; i < gtGridRet.deletedRecords.Length; i++)
                            //{
                            //    string stridtm = gtGridRet.deletedRecords[i]["PAGE_COL_ID"].ToString();
                            //    if (stridtm != string.Empty)
                            //    {
                            //        DataRow[] arrtemr = ds.Tables[0].Select("PAGE_COL_ID='" + stridtm + "'");
                            //        if (arrtemr.Length > 0)
                            //        {
                            //            arrtemr[0].Delete();
                            //        }
                            //    }
                            //}
                            if (ds.HasChanges())
                            {
                                //System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                                //transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                                //transactionOption.Timeout = new TimeSpan(0, 10, 0);
                                //using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                                //{ 
                                using (msBsc.AppLogic.DataAlc.CBizShpPageColumnItem objtem1 = new msBsc.AppLogic.DataAlc.CBizShpPageColumnItem())
                                {
                                    objtem1.Update(ds);
                                }
                                ds.AcceptChanges();

                                //    ts.Complete();
                                //    ts.Dispose();

                                //}
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        ds.RejectChanges();
                        gtGridRetSaveException = err.Message;
                    }
                }
                if (gtGridRetSaveSuccess && (this.gtGridRet.action == "save" || strAction == "btnSave"))//根据传入的回调类型进行处理，这里响应编辑按钮
                {
                    //保存后将页面置为查看状态
                    SetMode("VIEW");
                }
                //处理数据保存 
            }
            else if (strAction == "btnEdit")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("EDIT");
            }
            else if (strAction == "btnNotSave")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("VIEW");
            }
            else if (strAction == "btnConfirm")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                dobtnAddColList(this.GetParamHtValue("CHECK_ID"), this.GetParamHtValue("PAGE_NO"), this.GetParamHtValue("COL_ID"));
                this.ExecJScript("btnDo('mFinder');");
            }
            else if (strAction == "btnCancel")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                dobtnDelColList(this.GetParamHtValue("CHECK_ID"), this.GetParamHtValue("COL_ID"));
                this.ExecJScript("btnDo('mFinder');");
            } 

            base.InitialByParam();
        }

        private void dobtnDelColList(string strIds,  string strColId)
        {
            if (strIds == string.Empty ||   strColId == string.Empty)
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizShpPageColumnItem objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpPageColumnItem())
            {
                //System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                //transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                //transactionOption.Timeout = new TimeSpan(0, 10, 0);
                //using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                //{

                DataSet ds = objCBizShpOrderForm.SelectByColId(strColId);
                string[] arrstrtm = strIds.Split(',');
                for (int i = 0; i < arrstrtm.Length; i++)
                {
                    if (arrstrtm[i].Trim() != string.Empty)
                    {
                        DataRow[] arrdrtm = ds.Tables[0].Select("GOODS_ID='" + arrstrtm[i] + "'");
                        if (arrdrtm.Length > 0)
                        {
                            arrdrtm[0].Delete();
                        }
                    }
                }
                if (ds.HasChanges())
                {
                    objCBizShpOrderForm.Update(ds);
                    ds.AcceptChanges();

                }
                //    ts.Complete();
                //    ts.Dispose();
                //}
            } 
        }

        private void dobtnAddColList(string strIds ,string strPageNo,string strColId)
        {
            if (strIds == string.Empty || strPageNo == string.Empty || strColId == string.Empty)
            {
                return; 
            }
            using (msBsc.AppLogic.DataAlc.CBizShpPageColumnItem objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpPageColumnItem())
            {
                //System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                //transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                //transactionOption.Timeout = new TimeSpan(0, 10, 0);
                //using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                //{

                    DataSet ds = objCBizShpOrderForm.SelectByColId(strColId);
                    string[] arrstrtm = strIds.Split(',');
                    for (int i = 0; i < arrstrtm.Length; i++)
                    {
                        if (arrstrtm[i].Trim() != string.Empty)
                        {
                            DataRow[] arrdrtm = ds.Tables[0].Select("GOODS_ID='" + arrstrtm[i] + "'");
                            if (arrdrtm.Length <= 0)
                            {
                                DataRow drnew = ds.Tables[0].NewRow();
                                drnew["PAGE_COL_ITEM_ID"] = (new Epm.AppLogic.Framework.CDbAccess()).NewID();
                                drnew["GOODS_ID"] = arrstrtm[i];
                                drnew["PAGE_COL_ID"] = strColId;
                                drnew["PAGE_NO"] = strPageNo;
                                drnew["SEQ_NUM"] = ds.Tables[0].Rows.Count;
                                ds.Tables[0].Rows.Add(drnew);
                            }
                        } 
                    }
                    if (ds.HasChanges())
                    {
                        objCBizShpOrderForm.Update(ds);
                        ds.AcceptChanges();

                    }
                //    ts.Complete();
                //    ts.Dispose();
                //}
            } 
        }

         
        //修改页面状态
        private void SetMode(string strMode)
        {
            EditMode = strMode; switch (strMode)
            {
                case "EDIT":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    EnabledLinkBtn("btnNotSave", true);
                    EnabledLinkBtn("btnConfirm", false);
                    EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "ADD":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    EnabledLinkBtn("btnNotSave", false);
                    EnabledLinkBtn("btnConfirm", false);
                    EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "VIEW":
                    EnabledLinkBtn("btnEdit", true);
                    EnabledLinkBtn("btnSave", false);
                    EnabledLinkBtn("btnNotSave", false);
                    EnabledLinkBtn("btnAddSpecail", true);
                    EnabledLinkBtn("btnDelSpecail", true);
                    EnabledLinkBtn("btnConfirm", true);
                    EnabledLinkBtn("btnCancel", true);
                    this.ExecJScript("doCancelGt('myGrid1');");
                    break;
            }
        }
        //获取数据原,"DP_DATA"字符一定不要改变，否则系统不能自动清除缓存
        public DataSet GetDisplayData(string strgtGridID, string strSrcF, string strsRadom)
        {
            if (strgtGridID == "myGrid1")
            {
                DataSet ds;
                if (strSrcF == "mFinder" || !this.SessionIsExists(strsRadom + "DP_DATA", "1"))//点击查询按钮时，或需要数据而没有缓存时，根据条件取出数据记录
                {
                    string strCOMM_TENANT = this.GetParamHtValue("COMM_USER_NME");
                    string strGOODS_NME = this.GetParamHtValue("GOODS_NME");
                    string strIS_SELECTED = this.GetParamHtValue("IS_SELECTED");

                    string strMallNo = this.GetParamHtValue("MALL_NO");
                    string strColId = this.GetParamHtValue("COL_ID");
                    //using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                    //{
                    //    ds = oCDbAccess.QueryBySQL("SELECT * FROM SHP_GOODS_TYPE_LEVEL_ONE WHERE LEVEL_ONE_NME LIKE '%" + strLEVEL_ONE + "%' AND  SHOW_PAGE_URL LIKE '%" + strURL_CDE + "%'");
                    //    ds.Tables[0].TableName = "SHP_GOODS_TYPE_LEVEL_ONE";
                    //} 
                    using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpGoodsInfo())
                    {
                        string strsql = "";
                        strsql = @"
    SELECT TOP 100
    B.*
    ,SRC_GOODS_ID=A.GOODS_ID
    ,IS_SELECTED_TXT=CASE WHEN B.PAGE_COL_ITEM_ID IS NULL THEN '' ELSE '√' END
    ,A.MALL_NO
    ,A.COMM_TENANT_ID
    ,A.GOODS_NME
    ,A.IMG_URL
    ,A.VIEW_NUM
    ,A.SCORE_NUM
    ,A.GOODS_STRIKE_PRICE
    ,A.GOODS_STRIKE_SCORE
    ,A.IMG_URL
    ,C.COMM_TENANT_NME
    ,C.USER_NME
    ,GOODS_STATUS_TXT=CASE A.GOODS_STATUS_SID WHEN '9' THEN '已删除' WHEN '1' THEN '销售中' ELSE '已下架' END
    ,LEVEL_DESC=ISNULL(C1.LEVEL_ONE_NME,'')+'-'+ISNULL(C2.LEVEL_TWO_NME,'')+'-'+ISNULL(C3.LEVEL_THREE_NME,'')
    ,E.COMM_BRAND_DESC
    FROM SHP_GOODS_INFO A 
    LEFT OUTER JOIN SHP_PAGE_COLUMN_ITEM B ON A.GOODS_ID=B.GOODS_ID AND  B.PAGE_COL_ID='" + this.GetParamHtValue("COL_ID") + @"'
    LEFT OUTER JOIN SHP_COMMERCIAL_TENANT_INFO C ON A.COMM_TENANT_ID=C.COMM_TENANT_ID
    LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_ONE C1 ON A.LEVEL_ONE_ID=C1.LEVEL_ONE_ID
    LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_TWO C2 ON A.LEVEL_TWO_ID=C2.LEVEL_TWO_ID
    LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_THREE C3 ON A.LEVEL_THREE_ID=C3.LEVEL_THREE_ID
    LEFT OUTER JOIN SHP_COMMERCIAL_GOODS_BRAND E ON A.BRAND_ID = E.BRAND_ID
    WHERE 
    ";
                        if (strIS_SELECTED == "0")
                        {
                            strsql += " (A.GOODS_ID NOT IN (SELECT GOODS_ID FROM SHP_PAGE_COLUMN_ITEM WHERE PAGE_COL_ID='" + this.GetParamHtValue("COL_ID") + "'))";
                        }
                        if (strIS_SELECTED == "1")
                        {
                            strsql += " (A.GOODS_ID IN (SELECT GOODS_ID FROM SHP_PAGE_COLUMN_ITEM WHERE PAGE_COL_ID='" + this.GetParamHtValue("COL_ID") + "'))";
                        }
                        if (strMallNo != string.Empty)
                        {
                            strsql += "  AND (A.MALL_NO='" + strMallNo + "')";
                        }
                        if (strGOODS_NME != string.Empty)
                        {
                            strsql += "  AND (A.GOODS_NME LIKE '%" + strGOODS_NME + "%')";
                        }
                        if (strCOMM_TENANT != string.Empty)
                        {
                            strsql += "  AND ('" + strCOMM_TENANT + @"'='' OR A.COMM_TENANT_ID='" + strCOMM_TENANT + @"' OR C.COMM_TENANT_NME='" + strCOMM_TENANT + @"' OR C.USER_NME='" + strCOMM_TENANT + @"')";
                        }
                         

                        ds = objCBizShpOrderForm.DataObj.QueryBySQL(strsql);
                        ds.Tables[0].TableName = "SHP_PAGE_COLUMN_ITEM";
                        ds.AcceptChanges();
                    }
                    SetSession(strsRadom + "DP_DATA", ds, "1");
                    //查询时，将页面置为查看状态
                    if (strSrcF == "mFinder")
                    {
                        SetMode("VIEW");
                        if (this.GetParamHtValue("IS_SELECTED") == "0")
                        {
                            EnabledLinkBtn("btnEdit", false);
                            EnabledLinkBtn("btnSave", false);
                            EnabledLinkBtn("btnConfirm", true);
                            EnabledLinkBtn("btnCancel", false);
                        }
                        else
                        {
                            EnabledLinkBtn("btnEdit", true);
                            EnabledLinkBtn("btnSave", false);
                            EnabledLinkBtn("btnConfirm", false);
                            EnabledLinkBtn("btnCancel", true);
                        }
                    }
                }
                else
                {
                    ds = (DataSet)this.GetSession(strsRadom + "DP_DATA", "1");
                }
                return ds;
            }
            else
            {
                return null;
            }
        }

    }
}